const app = getApp()
import {
  HomeIndex
} from 'index-modal.js';
import {
  Config
} from '../../../utils/config.js';
var Function = require("../../../utils/function.js");
var index = new HomeIndex(); //实例化 首页 对象

Page({
  data: {
    activlen: [{
      name: '待消费'
    }, {
      name: '治疗中'
    }, {
      name: '已完成'
    }, {
      name: '已取消/过号'
    }],
    activeindex: 0,
    staarr: ['排队中', '服务中', '未评价', '', '', '已取消'],
    windowWidth: 0,
    is_hide: false,
    statused: 1,
    orderObj: [],
    yanmi_du: {},
    conlist: [], //优惠券
    url: Config.Url,
    myid: 4,
    c_id:'',
    c_p: '0.00',
    c_b: '请选择',
    model:false,
    indexssss:''
  },
  onLoad: function(options) {
    let that=this;
    if (app.globalData.state !=''){
      that.setData({
        activeindex: 2,
        statused: 3
      })
    }
    if (options.state==3){
      that.setData({
        activeindex:2,
        statused:3
      })
    }
    // let state = options.state;
    // if (state == 1) {
    //   this.setData({
    //     activeindex: 2
    //   })
    // }
    // let appactiveindex = app.globalData.activeindex
    // if (appactiveindex != 2) {
    //   let statused = options.status || 1
    //   this.setData({
    //     statused
    //   })
    // } else {
    //   let statused = 3
    //   this.setData({
    //     statused
    //   })
    // }

    // this._coupon()
  },
  godetail(e) {
    wx.navigateTo({
      url: '../detail/detail?id=' + e.currentTarget.dataset.id,
    })
  },

  radioChange:function(e){
    let that=this;
    // that.data.conlist[e.detail.value].type;
    
    that.setData({
      indexssss: e.detail.value,
      cou_id: that.data.conlist[e.detail.value].id,
      title_cou: that.data.conlist[e.detail.value].title,
    })
  },
  quedingBtn:function(){
    let that=this;
    if (that.data.indexssss==''){
      that.setData({
        model: false,
      })
    }else{
      let c_b = '满' + that.data.conlist[that.data.indexssss].man + '减' + that.data.conlist[that.data.indexssss].jian
      that.setData({
        model: false,
        c_b: c_b
      })
    }
    
  },
  showModel:function(e){
    let that=this;
    let index = Function.getDataSet(e,'index');
    that.setData({
      model:true,
      indexss: index
    })
    that._coupon(that.data.orderObj[index].id)
  },

  getIndex(e) {
    var activeindex = parseInt(e.currentTarget.dataset.index);
    if (activeindex == this.data.activeindex) {
      return;
    }
    this.setData({
      activeindex,
      statused: activeindex + 1,
      c_p: '0.00',
      c_b: "请选择",
    })
    this._getOrder()
  },

  _getOrder(fn) {
    let that = this;
    let uid = wx.getStorageSync('members').id
    let meny = wx.getStorageSync('meny')
    let data = {
      uid,
      status: that.data.statused,
      jingdu: meny.weidu || '',
      weidu: meny.jingdu || ''
    }
    index.getOrder(data, (res) => {
      if (res.code == 200) {
        if (that.data.statused == 1 || that.data.statused == 2) {
          var a = [];
          a.push(res.data)
          for (let i = 0; i < a.length; i++) {
            a[i].tt = (Number(a[i].total) - Number(a[i].bang_price)).toFixed(2);
          }
          
          that.setData({
            orderObj: a
          })
          // for (let i = 0; i < res.data.length; i++) {
          //   res.data[i].tt = (Number(res.data[i].total) - Number(res.data[i].bang_price) - Number(that.data.c_p)).toFixed(2);
          // }
          // that.setData({
          //   orderObj: res.data
          // })

        } else {
          for (let i = 0; i < res.data.length; i++) {
            res.data[i].tt = (Number(res.data[i].total) - Number(res.data[i].bang_price) - Number(that.data.c_p)).toFixed(2);
          }
          that.setData({
            orderObj: res.data
          })
        }
        if (fn) {
          fn()
        }
      } else {
        that.setData({
          orderObj: []
        })
      }

    })
  },
  payphone(e) { //电话联系
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.tel,
      success: function(e) {
      }
    })
  },
  _linespeed() { //排队
    let that = this;
    let data = {
      id: that.data.orderObj[0]['doctor_id']
    }
    index.linespeed(data, (res) => {
      if (res.code == 200) {
        that.setData({
          yanmi_du: res.data
        })
      }
    })
  },
  onShow: function() {
    let that = this;
    this._getOrder(() => {
      if (this.data.statused == 1) {
        that._linespeed()
      }
    })
    let kd = wx.getStorageSync('orderid') || [];
    if (!Array.isArray(kd)) {
      
    }
    // if (app.globalData.type && app.globalData.activeindex != 2) {
    //   that.setData({
    //     activeindex: 0
    //   })
    // } else if (app.globalData.activeindex == 2) {
    //   that.setData({
    //     activeindex: 2
    //   })
    // }
  },

  onHide: function() {
    let that = this;
    app.globalData.type = null;
    app.globalData.activeindex = null;
    app.globalData.state='';
  },

  gopay(e) {
    var orderid = e.currentTarget.dataset.orderid;
    var that = this;
    let data = {
      uid: wx.getStorageSync('members')['id'],
      order_id: orderid,
      coupon_id: that.data.cou_id,
      type: 1,
      form_id: e.detail.formId
    }
    index.wePayment(data, (res) => {
      if (res.code == 200) {
        wx.requestPayment({
          'timeStamp': res.data.timeStamp.toString(),
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          success: function(ress) {
            that._getOrder();
          },
          fail: function(err) {
            
            let datas={
              order_id: orderid,
              // coupon_id: that.data.c_id
              coupon_id: that.data.cou_id
            }
            index.save_coupon(datas,(ress)=>{
              
            })
          }
        })
      }
    })
    // if (this.data.conlist.length==0){

    // }else{
    //   wx.navigateTo({
    //     url: '/pages/my/coupon/coupon?type=1&price=' + that.data.orderObj[0].price
    //   })
    // }
  },

  _coupon(id) {
    let that = this;
    let uid = wx.getStorageSync('members').id;
    let data = {
      uid,
      order_id:id
    }
    index.coupon(data, (res) => {
      if (res.code == 200) {
        for (let i = 0; i < res.data.length; i++) {
          res.data[i].title = '满' + res.data[i].man + '减' + res.data[i].jian
        }
        that.setData({
          conlist: res.data
        })
      } else {
        that.setData({
          conlist: []
        })
      }
    })
  },

  //选择优惠劵
  selectCoup: function(e) {
    let that = this;
    let c_b = '-￥' + that.data.conlist[e.detail.value].jian
    that.setData({
      indexC: e.detail.value,
      c_id: that.data.conlist[e.detail.value].id,
      c_p: that.data.conlist[e.detail.value].jian,
      c_b: c_b,
    })
  },

  gomap(e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    wx.openLocation({
      latitude: Number(that.data.orderObj[index].weidu),
      longitude: Number(that.data.orderObj[index].jingdu),
      name: that.data.orderObj[index].goods_name,
      scale: 15,
      success: function() {
      }
    })
  },
  onPullDownRefresh: function () {
    let that=this;
    that.setData({
      orderObj:[]
    })
    that._getOrder()
    setTimeout(function () {
      wx.stopPullDownRefresh()
    }, 2000)
  },

  showModal() {
    this.setData({
      is_hide: true
    })
  },
  hideModal() {
    this.setData({
      is_hide: false
    })
  }

})